<template>
  <div>
    <el-form ref="sRef" :inline="true" :model="dictkeyCl.dictKeyS" class="demo-dynamic">
      <el-form-item label="字典名称：" prop="dictName">
        <el-input v-model="dictkeyCl.dictKeyS.dictName" placeholder="字典名称" type="text"></el-input>
      </el-form-item>
      <el-form-item label="字典编码：" prop="dictKey">
        <el-input v-model="dictkeyCl.dictKeyS.dictKey" placeholder="字典编码" type="text"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="resetS(sRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加字典类型
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="dictkeyCl.tableData" stripe style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (dictkeyCl.dictKeyS.page - 1) * dictkeyCl.dictKeyS.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="字典名称" prop="dictName" width="180" />
      <el-table-column align="center" header-align="center" label="字典编码" prop="dictKey" width="180" />
      <el-table-column align="center" header-align="center" label="字典类型" prop="dictType" width="180">
        <template #default="scope">
          <span v-if="scope.row.dictType === 0">主业务</span>
          <span v-if="scope.row.dictType === 1">其他</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column align="center" fixed="right" header-align="center" label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteDictKey(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="dictkeyCl.dialogDictKey" destroy-on-close title="添加字典类" @close="resetE(dictKeyEditRef)">
      <el-form ref="dictKeyEditRef" :model="dictkeyCl.dictKeyEdit" :rules="dictkeyCl.rules">
        <el-form-item label="父字典" prop="pid">
          <el-select v-model="dictkeyCl.dictKeyEdit.pid" placeholder="请选择字典">
            <el-option v-for="item in dictkeyCl.selectDictKey" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="dictkeyCl.dictKeyEdit.dictName" type="text"></el-input>
        </el-form-item>
        <el-form-item label="字典编码" prop="dictKey">
          <el-input v-model="dictkeyCl.dictKeyEdit.dictKey" type="text"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-select v-model="dictkeyCl.dictKeyEdit.dictType" placeholder="请选择字典类型">
            <el-option v-for="item in dictkeyCl.dictTypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="dictkeyCl.dictKeyEdit.sort" :min="1" />
        </el-form-item>
        <el-form-item autocomplete="off" label="备注" prop="remark">
          <Editor v-model:editorValue="dictkeyCl.dictKeyEdit.remark"></Editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存 </el-button>
          <el-button @click="dictkeyCl.dialogDictKey = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <Page :pageData="dictkeyCl.dictKeyS" @selectPage="search"></Page>
</template>
<script lang="ts" setup>
import { dictKeyClass, dictKeyInfo } from '@/page/types/system/dict/key'
import { ElMessage, FormInstance } from 'element-plus/es'
import { delDictKey, detailDictKey, pageDictKey, saveDictKey, selectDictKey, updateDictKey } from '@/api/system'
import Editor from '@/components/MyEditor.vue'
import { baseListInfo } from '@/page/types/base/baseList'
import Page from '@/components/Page.vue'

const dictkeyCl = reactive(new dictKeyClass())
const dictKeyEditRef = ref()
const sRef = ref()
const search = () => {
  pageDictKey(dictkeyCl.dictKeyS).then(res => {
    dictkeyCl.tableData = res.data.records as dictKeyInfo[]
    dictkeyCl.dictKeyS.page = res.data.current
    dictkeyCl.dictKeyS.size = res.data.size
    dictkeyCl.dictKeyS.total = res.data.total
  })
}
const resetS = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  search()
}
const opendialog = () => {
  dictkeyCl.dialogDictKey = true
  selectDictKey(null).then(res => {
    dictkeyCl.selectDictKey = res.data as baseListInfo[]
  })
}
const edit = (id: string) => {
  detailDictKey(id).then(res => {
    dictkeyCl.dialogDictKey = true
    dictkeyCl.dictKeyEdit = res.data as dictKeyInfo
  })
  selectDictKey(null).then(res => {
    dictkeyCl.selectDictKey = res.data as baseListInfo[]
  })
}
const deleteDictKey = (id: string) => {
  delDictKey(id).then(res => {
    search()
    ElMessage({
      message: '删除字典类型信息成功',
      type: 'success'
    })
  })
}
const resetE = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  dictkeyCl.dictKeyEdit = {} as dictKeyInfo
}
const save = () => {
  if (dictkeyCl.dictKeyEdit.id) {
    updateDictKey(dictkeyCl.dictKeyEdit).then(res => {
      search()
      ElMessage({
        message: '更新字典类型信息成功',
        type: 'success'
      })
    })
  } else {
    saveDictKey(dictkeyCl.dictKeyEdit).then(res => {
      search()
      ElMessage({
        message: '保存字典类型信息成功',
        type: 'success'
      })
    })
  }
  dictkeyCl.dialogDictKey = false
}
onMounted(() => {
  search()
})
</script>
<style lang="scss" scoped>
.pagination-block {
  position: relative;

  .el-pagination {
    margin-top: 20px;
    position: absolute;
    right: 0%;
  }
}
.el-select {
  width: 200px;
}
</style>
